package com.etone.generic;

import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Method;

public class GenericReflect<T> {

	/**  
	 * Generic use like the two function below
	 * T declared in class
	 * @param <T>
	 * @param args
	 * @return 
	 */
	T a;
	public T getInstanceT(T a){
		
		return this.a = a;
	}
	
	/**
	 * B haven't declared!
	 * @param b
	 * @return
	 */
	public <B> B getInstanceB(B b){
		
		return b;
	}
	
	public static <K> void inject(Class<K> clazz){
	    Method[] list =	clazz.getMethods();
		for(Method m : list){
			if (clazz == m.getDeclaringClass()) {
//				System.out.println(m.getName());
				try {
					//Object o = clazz.newInstance();
//						Object oo = m.invoke(new String());
					if(m.getName().endsWith("2"))
						m.invoke(clazz.newInstance(),"ddd");
					else 
						m.invoke(clazz.newInstance());
//						System.out.println((Invoke)m.invoke(clazz.newInstance()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
	}
	
	/**
	 * Use generic & reflect to generate now object
	 * @param clazz
	 * @return
	 */
	public static <O> O getObject(Class<O> clazz){
		try {
			return clazz.newInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void main(String[] args) {

		inject(Invoke.class);
	}

	static class Invoke{
		private String s ;
		public Invoke(){
			this.s = "Use " + Invoke.class.getSimpleName() + " to test!";
					
		}
		
		public void print(){
			System.out.println("Method 1-------->" + s);
		}
		public void print2(String d){
			System.out.println("Method 2-------->" + s);
		}
	}
}
